var editUrl = null;

$(function () {

});

var searchMenu = function () {
    $('#dg_menu').datagrid('load', {
        'menu.id': $('#searMenuParams input[name="menu.id"]').val(),
        'menu.name': $('#searMenuParams input[name="menu.name"]').val(),
        'menu.parent_id':$('#searMenuParams input[name="menu.parent_id"]').val()
    });
}

var closeEdit = function () {
    $('#dl_menu_edit').dialog("close");
}

var addMenu = function () {
    editUrl = ctx + "/sys/menu_add";
    $('#form_menu_edit input[name="menu.id"]').prop("readonly", false);
    $('#dl_menu_edit').dialog("open");
}
var updateMenu = function () {
    editUrl = ctx + "/sys/menu_update";
    var selected = $('#dg_menu').datagrid("getSelected");
    if (!selected) {
        $.messager.alert('系统提示', "请选择你要修改的菜单");
        return;
    }
    $('#form_menu_edit input[name="menu.id"]').val(selected.id);
    $('#form_menu_edit input[name="menu.id"]').prop("readonly", "readonly");
    $('#form_menu_edit input[name="menu.name"]').val(selected.name);
    $('#form_menu_edit input[name="menu.url"]').val(selected.url);
    $('#form_menu_edit input[name="menu.parent_id"]').val(selected.parent_id);
    $('#dl_menu_edit').dialog("open");
}

var submitMenu = function () {
    $('#form_menu_edit').form('submit', {
        url: editUrl,
        success: function (data) {
            data = eval('(' + data + ')');
            if (data && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
            closeEdit();
            $('#dg_menu').datagrid("reload");
        }
    });
}

var removeMenu = function () {
    var selected = $('#dg_menu').datagrid("getSelected");
    if (!selected) {
        $.messager.alert('系统提示', "请选择你要删除的菜单");
        return;
    }
    var id = selected.id;
    $.messager.confirm('系统提示', '您确定要删除菜单“' + selected.name + '”吗?', function (r) {
        if (r) {
            $.post(ctx + "/sys/menu_delete", {'menu.id': id}, function (data) {
                if (data && data.msg) {
                    $.messager.alert('系统提示', data.msg);
                }
                $('#dg_menu').datagrid("reload");
            });
        }
    });

}